package com.fanshuai.soring.boot.config;

import com.fanshuai.soring.boot.mvc.MyHandlerMapping;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

@Configuration
public class WebConfig extends WebMvcConfigurationSupport {
    @Override
    protected RequestMappingHandlerMapping createRequestMappingHandlerMapping() {
        return new MyHandlerMapping();
    }

    @Override
    protected void configureAsyncSupport(AsyncSupportConfigurer configurer) {
        configurer.setDefaultTimeout(1200 * 1000L);
        configurer.setTaskExecutor(asyncTaskExecutor());
    }

    private SimpleAsyncTaskExecutor asyncTaskExecutor() {
        SimpleAsyncTaskExecutor executor = new SimpleAsyncTaskExecutor();
        executor.setConcurrencyLimit(Runtime.getRuntime().availableProcessors());
        executor.setThreadNamePrefix("asyncTaskExecutor-");

        return executor;
    }
}
